package com.exer.dataload.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.commons.codec.binary.Base64;

import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * doris和mysql连接的配置类
 */
public final class DriverConfig {

    //doris连接
    private final static String DORIS_HOST = "192.168.138.103";
    //    private final static String DORIS_DB = "dorisDemo";
    private final static String DORIS_DB = "testDDL";
    private final static String DORIS_USER = "root";
    private final static String DORIS_PASSWORD = "123456";
    private final static String DORIS_HTTP_PORT = "8030";
    private final static String DORIS_JDBC_PORT = "9030";
    private final static String DORIS_STREAM_LOAD_URL_TEMPLATE = "http://%s:%s/api/%s/%s/_stream_load";
    //mysql连接
    private final static String MYSQL_USER = "root";
    private final static String MYSQL_PASSWORD = "Qq2830433136.";
    private final static String MYSQL_HOST = "192.168.1.207";
    private final static String MYSQL_PORT = "13306";
    private final static String MYSQL_DB = "dorisDemo";

    //mysql数据库连接池
    private final static DruidDataSource mysqlDataSource;
    //doris数据库连接池
    private final static DruidDataSource dorisDataSource;

    static {
        // 设置mysql连接池属性
        mysqlDataSource = new DruidDataSource();
        mysqlDataSource.setUrl(String.format("jdbc:mysql://%s:%s/%s?useSSL=false"
                , MYSQL_HOST, MYSQL_PORT, MYSQL_DB));
        mysqlDataSource.setUsername(MYSQL_USER);
        mysqlDataSource.setPassword(MYSQL_PASSWORD);
        mysqlDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        mysqlDataSource.setInitialSize(5); // 初始化时建立的连接数
        mysqlDataSource.setMaxActive(20);  // 最大连接池数量
        mysqlDataSource.setMinIdle(5);     // 最小闲置连接数
        mysqlDataSource.setMaxWait(60000); // 获取连接等待超时的时间
        // 设置doris连接池属性
        dorisDataSource = new DruidDataSource();
        dorisDataSource.setUrl(String.format("jdbc:mysql://%s:%s/%s?useUnicode=true&characterEncoding=utf8&useTimezone=true&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true"
                , DORIS_HOST, DORIS_JDBC_PORT, DORIS_DB));
        dorisDataSource.setUsername(DORIS_USER);
        dorisDataSource.setPassword(DORIS_PASSWORD);
        dorisDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dorisDataSource.setInitialSize(5); // 初始化时建立的连接数
        dorisDataSource.setMaxActive(20);  // 最大连接池数量
        dorisDataSource.setMinIdle(5);     // 最小闲置连接数
        dorisDataSource.setMaxWait(60000); // 获取连接等待超时的时间
    }

    public static String getDorisStreamLoadUrl(String tableName) {
        return String.format(DORIS_STREAM_LOAD_URL_TEMPLATE, DORIS_HOST, DORIS_HTTP_PORT, DORIS_DB, tableName);
    }

    public static String getDorisStreamLoadUrl(String dorisDB, String tableName) {
        return String.format(DORIS_STREAM_LOAD_URL_TEMPLATE, DORIS_HOST, DORIS_HTTP_PORT, dorisDB, tableName);
    }

    public static Connection getDorisConnection() throws SQLException {
        return dorisDataSource.getConnection();
    }

    public static Connection getMysqlConnection() throws SQLException {
        return mysqlDataSource.getConnection();
    }

    public static String basicAuthHeader() {
        String tobeEncode = DORIS_USER + ":" + DORIS_PASSWORD;
        byte[] encoded = Base64.encodeBase64(tobeEncode.getBytes(StandardCharsets.UTF_8));
        return "Basic " + new String(encoded);
    }

}
